<template>
  <div class="mod-api-userBalance">
    <avue-crud
      ref="crud"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot-scope="scope" slot="menuLeft">
        <el-button
          v-if="isAuth('user:userBalance:save') && dataList.length < 10"
          icon="el-icon-plus"
          type="primary"
          size="small"
          @click="addOrUpdateHandle()"
        >{{$t('crud.addTitle')}}</el-button>
      </template>
      <template slot-scope="scope" slot="img">
        <img :src="resourcesUrl + scope.row.img" width="280" height="130" />
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="isAuth('user:userBalance:update')"
          icon="el-icon-edit"
          size="small"
          type="primary"
          @click="addOrUpdateHandle(scope.row.balanceId)"
        >{{$t('crud.updateBtn')}}</el-button>
        <el-button
          v-if="isAuth('user:userBalance:delete')"
          type="danger"
          icon="el-icon-delete"
          size="small"
          @click="deleteHandle(scope.row.balanceId)"
        >{{$t('crud.delBtn')}}</el-button>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
import { tableOption } from '@/crud/user/userBalance'
import AddOrUpdate from './userBalance-add-or-update'
export default {
  data () {
    return {
      dataList: [],
      searchForm: {}, // 搜索
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/user/userBalance/getList'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.dataList = data
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$t('seckill.isDeleOper'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/user/userBalance/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('groups.successfulOperation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    }
  }
}
</script>
<style lang="scss">
.mod-api-userBalance {
  /deep/ .el-table__fixed-right {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  /deep/ .el-table__fixed{
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
}
</style>
